package
{

    import flash.desktop.NativeApplication;
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Loader;
    import flash.display.PNGEncoderOptions;
    import flash.display.Shape;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.IOErrorEvent;
    import flash.events.InvokeEvent;
    import flash.filesystem.File;
    import flash.filesystem.FileMode;
    import flash.filesystem.FileStream;
    import flash.geom.Rectangle;
    import flash.net.URLRequest;
    import flash.text.TextField;
    import flash.utils.ByteArray;

    [SWF(width="500", height="500", backgroundColor="#FFFFFF", frameRate="24")]
    public class MaskMovieCard extends Sprite
    {
        private var mScreenLog:TextField;

        private var mCacheWrapSp:Sprite;
        private var mImgFileInputFolder:String;
        private var mIngFileOutputFolder:String;


        private var mAllSubImgFileArray:Array;

        private var mCurrentTargetImg:Bitmap;
        private var mCurrentTargetImgLoader:Loader;
        private var mCurrentTargetImgLoaderRequest:URLRequest;

        private var mCurrentImgFileName:String;


        function MaskMovieCard()
        {
            mCacheWrapSp = new Sprite();
            mScreenLog = new TextField();
            mScreenLog.text = "处理中.....";
            this.addChild(mScreenLog);
            this.addChild(mCacheWrapSp);
            NativeApplication.nativeApplication.addEventListener(InvokeEvent.INVOKE, handleOnInvoke);
        }


        private function handleOnInvoke(event:InvokeEvent):void
        {
            mImgFileInputFolder = event.arguments[0];
            mIngFileOutputFolder = event.arguments[1];
            mAllSubImgFileArray = new File(mImgFileInputFolder).getDirectoryListing();
            tryToHandleNextImgFile();
        }

        private function tryToHandleNextImgFile():void
        {
            if (mAllSubImgFileArray.length > 0)
            {
                var imgFile:File = mAllSubImgFileArray.pop();
                if (imgFile.name.indexOf("jpg") >= 0)
                {
                    handleEachImgFile(imgFile);
                }
                else
                {
                    tryToHandleNextImgFile();
                }
            }
            else
            {
                mScreenLog.text = "全部处理完毕";
                //全部完成 退出
            }
        }

        private function handleEachImgFile(_imgFile:File):void
        {
            mCurrentImgFileName = _imgFile.name.replace("jpg", "png");
            mCurrentTargetImgLoaderRequest = new URLRequest(_imgFile.url);
            mCurrentTargetImgLoader = new Loader();
            mCurrentTargetImgLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onCurrentImgLoadComplete);
            mCurrentTargetImgLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onCurrentImgLoadInError);
            mCurrentTargetImgLoader.load(mCurrentTargetImgLoaderRequest);
        }

        private function onCurrentImgLoadInError(e:Event):void
        {
            mScreenLog.text = "无法加载当前图片,异常!";
        }

        private function onCurrentImgLoadComplete(e:Event):void
        {
            mCurrentTargetImg = Bitmap(mCurrentTargetImgLoader.content);
            handleImgCut(400, mIngFileOutputFolder + "/big_" + mCurrentImgFileName);
            handleImgCut(240, mIngFileOutputFolder + "/small_" + mCurrentImgFileName);
            mCurrentTargetImg.bitmapData.dispose();

            tryToHandleNextImgFile();
        }

        private function handleImgCut(_cutSize:int, _filePath:String):void
        {
            var mTargetFile:File = new File(_filePath);
            mTargetFile.exists && mTargetFile.deleteFile();
            if (!mTargetFile.exists)
            {
                //缩放到相应大小
                mCurrentTargetImg.width = mCurrentTargetImg.height = _cutSize;
                mCacheWrapSp.addChild(mCurrentTargetImg);

                //设置遮罩
                var mask:Shape = new Shape();
                mask.graphics.beginFill(0);
                mask.graphics.drawCircle(_cutSize / 2, _cutSize / 2, _cutSize / 2);
                mask.graphics.endFill();
                mCurrentTargetImg.mask = mask;

                //Cache出新的图片
                var newBitmapData:BitmapData = new BitmapData(_cutSize, _cutSize, true, 0);
                newBitmapData.draw(mCacheWrapSp);

                //编码为PNG格式
                var imgByteArray:ByteArray = newBitmapData.encode(new Rectangle(0, 0, _cutSize, _cutSize),
                                                                  new PNGEncoderOptions());
                //存储到本地
                var fileStream:FileStream = new FileStream();
                fileStream.open(mTargetFile, FileMode.WRITE);
                fileStream.writeBytes(imgByteArray);
                fileStream && fileStream.close();
                fileStream = null;
            }
        }


    }
}
